"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : DDL-table
Case Name   : 列存表在事务中新增列后回滚事务，再次重做事务再回滚，
              两次回滚执行成功              
Create At   : 2023/04/03
Owner       : @kyeleze
Description :
    1. 创建列存表并插入数据
    2. 执行事务并回滚
    3. 再次执行事务并回滚
Expect      :
    1. 成功
    2. 事务执行回滚成功
    3. 事务执行回滚成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Table(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.dirname(__file__)} start----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.table = 't_ddl_table_0333'

    def test_table(self):
        step = "--step1:创建列存表并插入数据 expect:成功--"
        self.log.info(step)
        sql_cmd = f'drop table if exists {self.table};' \
                  f'create table {self.table}(col1 int) ' \
                  f'with(orientation = column);' \
                  f'insert into {self.table} values(generate_series(1, 10));'
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS,
                      result, '执行失败' + step)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS,
                      result, '执行失败' + step)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG,
                      result, '执行失败' + step)

        step = "--step2:执行事务并回滚 expect:事务执行回滚成功--"
        self.log.info(step)
        sql_cmd = f'begin;' \
                  f'alter table {self.table} add col2 boolean default true;' \
                  f'select * from {self.table};' \
                  f'rollback;'
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.ROLLBACK_MSG, result, "执行失败" + step)

        step = "--step3:再次执行事务并回滚 expect:事务执行回滚成功--"
        self.log.info(step)
        sql_cmd = f'begin;' \
                  f'alter table {self.table} add col2 boolean default true;' \
                  f'select * from {self.table};' \
                  f'rollback;'
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.ROLLBACK_MSG, result, "执行失败" + step)

    def tearDown(self):
        self.log.info('--清理环境--')
        result = self.primary_sh.execut_db_sql(f'drop table {self.table};')
        self.log.info(result)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS,
                      result, "清理环境失败")
        self.log.info(f'----{os.path.dirname(__file__)} end----')

